package cn.jxau.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.jxau.bean.po.Article;
import cn.jxau.bean.po.ArticleType;
import cn.jxau.bean.po.User;
import cn.jxau.bean.po.UserCollections;
import cn.jxau.bean.po.UserCollectionsType;
import cn.jxau.mapper.UserCollectionsTypeMapper;
import cn.jxau.service.UserCollectionsService;
import net.sf.jsqlparser.statement.select.Select;

@Service
public class UserCollectionsServiceImpl extends AbstractCommonService implements UserCollectionsService {
	@Override
	public List<UserCollectionsType> getUserArticleType(Long userID) {
		return collectionsTypeMapper.selectList(new EntityWrapper<UserCollectionsType>().eq("userID", userID));
	}

	@Override
	public List<Article> getCollectionByUserID(Page<Article> page, Long id) {
		return userCollectionsMapper.getCollectionByUserID(page, id);
	}

	@Override
	public Long insertType(UserCollectionsType collectionsType) {
		UserCollectionsType type = null;
		UserCollectionsType parent = null;
		UserCollectionsType entity = null;
		if(collectionsTypeMapper.insert(collectionsType)>0){
			//此处没有做null 判断 因为如果插入成功 查询则不会为空 如果出错 可以优先检查这里
			type = collectionsTypeMapper.selectOne(collectionsType);
			//根结点为虚拟 跳过根节点
			if(type.getPid()!=1){
				parent = collectionsTypeMapper.selectById(type.getPid());
				//判断该节点是否是子节点 
				if(!parent.getHasItem()){
					entity = new UserCollectionsType();
					entity.setId(type.getPid());
					entity.setHasItem(true);
					if(collectionsTypeMapper.updateById(entity)>0){
						return (Long)type.getId();
					}
				}
			}
		}
		return (Long)type.getId();
	}

	/**
	 * 递归删除父节点以及所有子节点
	 * */
	@Override
	public int deleteTypeParentById(Long id,boolean isParent,Long UserID) {
		int returnFlag=1;
		UserCollectionsType type = collectionsTypeMapper.selectById(id);
		List<UserCollectionsType> list = getUserArticleType(UserID);
		for (UserCollectionsType userCollectionsType : list) {
			System.out.println(userCollectionsType);
		}
		try {
			if(isParent){
				for(int i=0;i<list.size();i++){
					if(id.equals(list.get(i).getPid())){
						deleteTypeParentById(list.get(i).getId(), type.getHasItem(),UserID);
					}
				}
				collectionsTypeMapper.deleteById(id);
			}else{
				deleteTypeSonById(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnFlag=0;
		}
		return returnFlag;
	}
	
	@Override
	public int deleteTypeSonById(Long id) {
		return collectionsTypeMapper.deleteById(id);
	}

	@Override
	public int updateTypeNameById(UserCollectionsType userCollectionsType) {
		return collectionsTypeMapper.updateById(userCollectionsType);
	}

	@Override
	public int deleteCollectionByID(Long articleID,Long userID) {
		return userCollectionsMapper.deleteByDoubleId(articleID,userID);
	}

	@Override
	public List<Article> getUserArticleByTypeID(Page<Article> page, Long usertypeID,Long userID) {
		return userCollectionsMapper.getUserArticleByTypeID(page, usertypeID, userID);
	}

	@Override
	public List<Article> getUserArticleByParentID(Page<Article> page, Long pid, Long userID) {
		return collectionsTypeMapper.getUserArticleByParentID(page, pid, userID);
	}

	@Override
	public int insertCollectionByID(Long articleID,Long usertypeID,Long userID) {
		List<UserCollections> isExist = userCollectionsMapper.selectIsExist(articleID,userID);
		System.out.println(isExist);
		UserCollections userCollections = null;
		if(isExist.size()>=1){
			return 0;
		}else{
			userCollections = new UserCollections();
			userCollections.setArticleID(articleID);
			userCollections.setUsertypeID(usertypeID);
			userCollections.setUserID(userID);
			return userCollectionsMapper.insert(userCollections);
		}
	}
}
